using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.WebControls
{
    [ToolboxData("<{0}:TextAreaField runat=server></{0}:TextAreaField>")]
    public class TextAreaField : BaseDataField
    {
        protected override DataControlField CreateField()
        {
            return new TextAreaField();
        }

        private int rows;

        public int Rows
        {
            get { return rows; }
            set { rows = value; }
        }

        private int cols;

        public int Cols
        {
            get { return cols; }
            set { cols = value; }
        }

        private int maxLength;

        public int MaxLength
        {
            get { return maxLength; }
            set { maxLength = value; }
        }
	
	
        protected override void InitEditControls(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            TextBox txt = new TextBox();
            txt.ID = GetUniqueId(rowIndex, "txt");
            txt.TextMode = TextBoxMode.MultiLine; 
            txt.Rows = this.Rows;
            txt.Columns = this.Cols;
            cell.Controls.Add(txt);
        }

        protected override void DataBindEditControls(DataControlFieldCell cell, object dataItem, int dataItemIndex, int displayIndex)
        {
            if (cell != null && cell.Controls.Count > 0)
            {
                TextBox txt = cell.Controls[0] as TextBox;
                if (txt != null)
                {
                    object val = DataBinder.Eval(dataItem, DataField);
                    if (val != null)
                    {
                        txt.Text = val.ToString();
                    }
                }
            }
        }

        protected override void DataBindDisplayControls(DataControlFieldCell cell, object dataItem, int dataItemIndex, int displayIndex)
        {
            if (cell != null && cell.Controls.Count > 0)
            {
                Label lbl = cell.Controls[0] as Label;
                if (lbl != null)
                {
                    object val = DataBinder.Eval(dataItem, DataField);
                    if (val != null)
                    {
                        lbl.Text = val.ToString().Replace("\n", "<br/>");
                    }
                }
            }
        }

        public override void ExtractValuesFromCell(System.Collections.Specialized.IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
        {
            if (cell.Controls.Count > 0 && !string.IsNullOrEmpty(this.DataField))
            {
                TextBox txt = cell.Controls[0] as TextBox;
                if (txt != null)
                {
                    dictionary.Add(this.DataField, txt.Text);
                }
            }
        }
    }
}
